<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="110px"
      v-loading="formLoading"
    >
      <el-form-item label="账户类型" prop="accountType">
        <el-select v-model="formData.accountType" placeholder="请选择账户类型">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.BILL_ACCOUNT_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="hasEnable">
        <el-radio-group v-model="formData.hasEnable">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.BILL_YES_OR_NO)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="启用日期" prop="accountStartTime">
        <el-date-picker
          v-model="formData.accountStartTime"
          type="date"
          value-format="x"
          placeholder="选择账号启用日期"
        />
      </el-form-item>
      <el-form-item label="停用日期" prop="accountStopTime">
        <el-date-picker
          v-model="formData.accountStopTime"
          type="date"
          value-format="x"
          placeholder="选择账号停用日期"
        />
      </el-form-item>
      <el-form-item label="账号名称" prop="accountName">
        <el-input v-model="formData.accountName" placeholder="请输入账号名称" />
      </el-form-item>
      <el-form-item label="账户主人" prop="accountOwner">
        <el-input v-model="formData.accountOwner" placeholder="请输入账户主人" />
      </el-form-item>
      <el-form-item label="资金归属" prop="moneyOwner">
        <el-input v-model="formData.moneyOwner" placeholder="请输入资金归属" />
      </el-form-item>

      <el-form-item label="银行名称" prop="bankName">
        <el-select v-model="formData.bankName" placeholder="请选择银行名称">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.BILL_BANK_NAME)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="银行卡号" prop="bankCard">
        <el-input v-model="formData.bankCard" placeholder="请输入银行卡号" />
      </el-form-item>
      <el-form-item label="计入流水" prop="hasWater">
        <el-radio-group v-model="formData.hasWater">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.BILL_YES_OR_NO)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="账号备注" prop="accountRemark">
        <el-input v-model="formData.accountRemark" placeholder="请输入账号备注" />
      </el-form-item>
      <el-form-item label="排序号" prop="orderNum">
        <el-input-number v-model="formData.orderNum" :step="10" />
      </el-form-item>
      <el-form-item label="逻辑父账户" prop="logicParentAccount">
        <el-input v-model="formData.logicParentAccount" placeholder="请输入逻辑父账户" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { AccountApi, AccountVO } from '@/api/bill/account'

/** 账户信息 表单 */
defineOptions({ name: 'AccountForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  accountId: undefined,
  accountName: undefined,
  accountOwner: undefined,
  accountRemark: undefined,
  accountStartTime: undefined,
  accountStopTime: undefined,
  hasWater: undefined,
  hasEnable: undefined,
  moneyOwner: undefined,
  accountType: undefined,
  bankName: undefined,
  bankCard: undefined,
  orderNum: undefined,
  logicParentAccount: undefined,
  remarkBalance: undefined,
  remarkBalanceTime: undefined,
  extParam: undefined
})
const formRules = reactive({
  accountType: [{ required: true, message: '账户类型不能为空', trigger: 'blur' }],
  accountOwner: [{ required: true, message: '账户主人不能为空', trigger: 'blur' }],
  moneyOwner: [{ required: true, message: '资金归属不能为空', trigger: 'blur' }],
  hasEnable: [{ required: true, message: '是否启用不能为空', trigger: 'blur' }],
  hasWater: [{ required: true, message: '是否流水不能为空', trigger: 'blur' }],
  accountName: [{ required: true, message: '账号名称不能为空', trigger: 'blur' }],
  accountRemark: [{ required: true, message: '账号备注不能为空', trigger: 'blur' }],
  accountStartTime: [{ required: true, message: '账号启用日期不能为空', trigger: 'blur' }],
  accountStopTime: [{ required: true, message: '账号停用日期不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await AccountApi.getAccount(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as AccountVO
    if (formType.value === 'create') {
      await AccountApi.createAccount(data)
      message.success(t('common.createSuccess'))
    } else {
      await AccountApi.updateAccount(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    accountId: undefined,
    accountName: undefined,
    accountOwner: undefined,
    accountRemark: undefined,
    accountStartTime: undefined,
    accountStopTime: undefined,
    hasWater: undefined,
    hasEnable: undefined,
    moneyOwner: undefined,
    accountType: undefined,
    bankName: undefined,
    bankCard: undefined,
    orderNum: undefined,
    logicParentAccount: undefined,
    remarkBalance: undefined,
    remarkBalanceTime: undefined,
    extParam: undefined
  }
  formRef.value?.resetFields()
}
</script>
